package com.ld.security.common.mybatis.base.service;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ld.security.common.core.base.entity.ESuperEntity;
import com.ld.security.common.core.base.po.*;
import com.ld.security.common.core.base.service.ISuperService;
import com.ld.security.common.core.base.vo.PageVo;
import com.ld.security.common.mybatis.base.po.IdsPo;
import com.ld.security.common.mybatis.core.wrapper.WrapperFactory;
import com.ld.security.common.mybatis.core.wrapper.EntityWrapper;
import com.ld.security.common.mybatis.base.mapping.SuperMapping;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;


public abstract class ISuperServiceImpl<E extends ESuperEntity> implements ISuperService<E> {

    @Autowired
    protected SuperMapping<E> superMapping;

    /**
     * 拦截链,对象预处理使用
     */
    @Autowired
    WrapperFactory wrapperFactory;


    @Override
    public  List<E> all(SuperPo po) {
        EntityWrapper ew =  this.getWrapper(po);
        List<E> list =  this.superMapping.selectList(ew);

        return list;
    }




    @Override
    public List<E> all() {
        return this.all( new ListPo<>());
    }

    @Override
    public List<E> listByIds(Collection<String> ids) {
        return all(new IdsPo(ids));
    }

    @Override
    public int save(E entity) {
        /**
         * 添加预处理 加密 转义
         */



        return superMapping.save(entity);
    }

    @Override
    public int saveBatch(Collection<E> entityList) {
        int total = 0;
        for (E e : entityList) {
            total += this.save(e);
        }
        return total;
    }

    @Override
    public E getById(String id) {
        List<E> list =  this.listByIds(Arrays.asList(id));
        return list.isEmpty()?null:list.get(0);
    }





    @Override
    public int remove(SuperPo query) {
        /**
         * 删除预处理
         */

        EntityWrapper<E> ew =  this.getWrapper(query);
        int total = superMapping.delete(ew);
        return total;
    }





    @Override
    public PageVo<E> page(SuperPo po) {

        //查询对象预处理

        Page<E> page = this.toPage(po);
        EntityWrapper ew =  this.getWrapper(po);
        superMapping.page(ew,page);

        PageVo vo =  this.toPageVo(page);


        return vo;
    }

    @Override
    public int count(SuperPo query) {
        EntityWrapper ew =  this.getWrapper(query);
        return this.superMapping.count(ew);
    }

    protected PageVo<E> toPageVo(Page page){
        PageVo vo = new PageVo<>();
        vo.setTotal(page.getTotal());
        vo.setList(page.getRecords());
        return vo;
    }


    protected ListPo<SuperPo> toListVo(SuperPo po){
        ListPo<SuperPo> vo = new ListPo<>();
        vo.setQuery(po);
        return vo;
    }

    public Page toPage(SuperPo po) {
        Page page = new Page();
        if(po instanceof LimitPo){
            LimitPo p =(LimitPo) po;
            page.setCurrent(p.getPageNumber());
            page.setSize(p.getPageSize());
        }
        return page;
    }



    protected EntityWrapper getWrapper(SuperPo po){

        return wrapperFactory.getWrapper(po);
    }



    protected  EntityWrapper getWrapper(){
        return wrapperFactory.getWrapper();
    }
}
